package tw.fc.agui;

import android.view.MotionEvent;

//[  依Container的規律, 0 在頂層, 隨index漸增而漸深. 後進的在底層.
public class GrObjManager extends java.util.ArrayList<GrObj>
    // implements MouseInputListener //: 由GraphicPanel呼叫反應函數
{
   private static final long serialVersionUID = 2010062907L;

   protected final GrPanel thePanel;
   protected final GrObj theObj;//新增

   private boolean _mouseInside=false;
   //protected static boolean _mouseInside=false;
   private GrObj _passedObj=null;
   private GrObj _holdedObj=null;
   private GrObj _lastHoldedObj=null;
     //: _lastHoldedObj專供mouseClicked用, 由mouseReleased設定. 
   private int _GraphicObjCount =0;

   //[ ---------- construct ----------
   public   GrObjManager(GrPanel p, int capacity) {
      super(capacity);
      thePanel=p;
      theObj = null;
   }   
   public   GrObjManager(int capacity) {
      super(capacity);
      thePanel=null;
      theObj = null;
   }
   public   GrObjManager(GrObj p, int capacity) {
      super(capacity);
      theObj=p;
      thePanel=null;
   }
   public   GrObjManager(GrPanel p) {  this(p,50); }
   public   GrObjManager(GrObj p)   {  this(p,50);  }   
   //] ---------- construct ----------
   
   public void toTop(GrObj ob) {
      final int idx=indexOf(ob);
      if(idx<0) {
         throw new IllegalArgumentException("object not in the panel");
      }
      if(idx==0) return;  // 本來就在first
      // remove(idx);   add(0,ob);
      for(int i=idx-1; i>=0; i--) {
         set(i+1,get(i));    //: replace
      }  set(0,ob);
   }

   public void toBottom(GrObj ob) {
      final int idx=indexOf(ob);
      if(idx<0) {
         throw new IllegalArgumentException("object not in the panel");
      }
      if(idx==size()-1) return;  // 本來就在last
      // remove(idx);   add(ob);
      for(int i=idx+1; i<size(); i++) {
         set(i-1,get(i));    //: replace
      }  set(size()-1,ob);
   }

   public GrObj findObjAt(ImuV2D at) { //: 頂層先找
      for(int i=0; i<size(); i++) {
        final GrObj ob=get(i);
        if(ob.visible && ob.contains(at)) return ob;
      }
      return null;
   }

   //[ **
   //[ 將事件轉發配到 GrObjM對應的處理函數
   //[ GrObjM會在轉發配一次給當前操作的繪圖物件
   //[ 繪圖物件針對各種事件, 去做反應.
   public void onFling(MotionEvent e1, MotionEvent e2, 
                       float velocityX, float velocityY) {
      if(_holdedObj!=null) {
         _holdedObj.forFling(e1, e2, velocityX, velocityY);
         GrObj.lastMousePos.setBy(e2);
      }
      GrObj.lastMousePos.setBy(e2);
   }
   public void onScroll(MotionEvent e1, MotionEvent e2, 
                        float velocityX, float velocityY) {
      if(_holdedObj!=null) {
         _holdedObj.forScroll(e1, e2, velocityX, velocityY);
         GrObj.lastMousePos.setBy(e2);
      }
      GrObj.lastMousePos.setBy(e2);
   }
   public void onDown(MotionEvent e) {
      GrObj.lastMousePos.setBy(e); //: 因未變位而不必, 但怕未manage move              
      final GrObj ob=findObjAt(GrObj.lastMousePos);
      if(ob!=null) {
         _holdedObj=ob;
         _holdedObj.forDown(e);  // obj可自己呼叫toTop
         GrObj.lastMousePos.setBy(e);
      }
   }
   public void onDoubleTap(MotionEvent e) {
      if(_holdedObj!=null) {
         _holdedObj.forDoubleTap(e);
         GrObj.lastMousePos.setBy(e);
      }
      GrObj.lastMousePos.setBy(e);
   }
   public void onDoubleTapEvent(MotionEvent e) {
      if(_holdedObj!=null) {
         _holdedObj.forDoubleTapEvent(e);
         GrObj.lastMousePos.setBy(e);
      }
      GrObj.lastMousePos.setBy(e);
   }
   public void onSingleTapConfirmed(MotionEvent e) {
      //: clicked只能緊接發生於pressed-released之後
      if(_lastHoldedObj!=null) {
         _lastHoldedObj.forSingleTapConfirmed(e);
         _lastHoldedObj=null;
      }
      GrObj.lastMousePos.setBy(e); //: 因未變位而不必, 但怕未manage 其它動作
   }
   public void onSingleTapUp(MotionEvent e) {
      if(_holdedObj!=null) {
         _holdedObj.forSingleTapUp(e);
         GrObj.lastMousePos.setBy(e);
      }
      GrObj.lastMousePos.setBy(e);
   }
   public void onLongPress(MotionEvent e) {
      if(_holdedObj!=null) {
         _holdedObj.forLongPress(e);
         GrObj.lastMousePos.setBy(e);
      }
      GrObj.lastMousePos.setBy(e);
   }
   public void onShowPress(MotionEvent e) {
      if(_holdedObj!=null) {
         _holdedObj.forShowPress(e);
         GrObj.lastMousePos.setBy(e);
      }
      GrObj.lastMousePos.setBy(e);
   }
   public void onMove(MotionEvent e) {
      if(_holdedObj!=null) {
         _holdedObj.forMove(e);
         GrObj.lastMousePos.setBy(e);
      }
      GrObj.lastMousePos.setBy(e);
   }   
   public void onRelease(MotionEvent e) {
      if(_holdedObj!=null) {
         _holdedObj.forRelease(e);
         _lastHoldedObj=_holdedObj;  //: 給 SingleTapUp用
         _holdedObj=null;
      }
      GrObj.lastMousePos.setBy(e); //: 因未變位而不必, 但怕未manage drag    
   }
   public void onMultiTouchDown(MotionEvent e) {
      if(_holdedObj!=null) {
         _holdedObj.forMultiTouchDown(e);
         _lastHoldedObj=_holdedObj;  
         _holdedObj=null;
      }
      GrObj.lastMousePos.setBy(e);   
   }
   public void onMultiTouchUp(MotionEvent e) {
      if(_holdedObj!=null) {
         _holdedObj.forMultiTouchUp(e);
         _lastHoldedObj=_holdedObj;  
         _holdedObj=null;
      }
      GrObj.lastMousePos.setBy(e); 
   }
   //]
}